const { update, remove, add, getCount, query, getOne } = require('../../utils/dbTools');
const { queryMemberInfo } = require('./staff')
const tableName = 'appointment';
const staffTableName = 'staff';


/**
 * 获取数据总条数
 * 
*/
const appointmentTotal = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctorid } = req.query;
    if (!doctorid) {
        output.code = 500;
        output.msg = "参数错误:缺少doctorid";
        return res.send(output);
    }
    const result = await getCount(tableName, { doctorid });
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
    }
    output.data = result;
    res.send(output);
}

/**
 *  根据医生id||患者id获取预约列表
 * 
*/
const appointmentList = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { doctorid, patientid, page, size } = req.query;
    if (!doctorid && !patientid) {
        output.code = 500;
        output.msg = "参数错误:缺少doctorid和patientid, 必须提供其中之一";
        return res.send(output);
    }
    // const map = { page, size };
    // const result = await getAll(tableName, map);
    let sql = `select a.id,a.patientid,a.doctorid,a.name,a.phone,a.appoint_time,a.status,a.create_time,s.name as doctor,s.phone as doctorPhone`
    sql += ` from ${tableName} a `;
    sql += ` inner join ${staffTableName} s on a.doctorid=s.id`;
    if (doctorid) {
        sql += ` where a.doctorid=${doctorid}`;
    } else {
        sql += ` where a.patientid=${patientid}`;
    }
    sql += ` order by a.id desc`;
    // 如果传递了分页参数, 则实现分页查询
    if (page && size) {
        sql += `  limit ${(page - 1) * size},${size}`;
    }
    const result = await query(sql);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    output.data = result;
    res.send(output);
}

/**
 * 新增预约信息
 * 
*/

const appointmentAdd = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { patientid, doctorid, name, phone, appoint_time } = req.body;
    console.log(appoint_time, 'appoint_time');

    if (!patientid || !doctorid || !name || !phone || !appoint_time) {
        output.code = 500;
        output.msg = "参数错误: 缺少patientid,doctorid,name,phone,appoint_time";
        return res.send(output);
    }
    // 将日期字符串转为时间戳
    appoint_time = new Date(appoint_time).getTime();
    const data = {
        patientid, doctorid, name, phone, appoint_time, create_time: Date.now()
    }
    const result = await add(data, tableName);
    if (!result) {
        output.code = 500;
        output.msg = '操作失败';
        return res.send(output);
    }
    res.send(output);
}

/**
 *  删除预约信息
 * 
*/

const appointmentRemove = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }

    const { id } = req.query;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误";
        return res.send(output);
    }

    const result = await remove(id, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}

/**
 * 更新预约信息: 仅支持更新状态和时间
*/
const appointmentUpdate = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    let { id, appoint_time, status } = req.body;
    if (!id) {
        output.code = 500;
        output.msg = "参数错误: 缺少参数id";
        return res.send(output);
    }
    const options = {}
    if (status) {
        options.status = status;
    }
    if (appoint_time) {
        // 将日期字符串转为时间戳
        options.appoint_time = new Date(appoint_time).getTime();
    }
    const result = await update(id, options, tableName);
    if (!result) {
        output.code = 500;
        output.msg = "操作失败";
        return res.send(output);
    }
    res.send(output);
}


/**
 * 获取预约详情
 * id: 预约id
 */
const appointmentInfo = async (req, res) => {
    const output = {
        code: 200,
        msg: '操作成功'
    }
    const { id } = req.query;
    if (!id) {
        output.msg = '参数错误， 缺少参数id'
        return res.send(output);
    }
    const result = await getOne(tableName, { id });
    if (!result) {
        output.code = 500;
        output.msg = '未查询到相关数据';
        return res.send(output);
    }
    output.data = result;
    // 查询医生信息
    const doctor = await queryMemberInfo(result.doctorid);
    if (doctor) {
        doctor.tags=doctor.tags?doctor.tags.split(','):doctor.tags
        output.data.doctor = doctor;
    }
    const patient = await queryMemberInfo(result.patientid);
    // 查询病人信息
    if (patient) {
        output.data.patient = patient;
    }
    res.send(output);
}

module.exports = {
    appointmentAdd,
    appointmentRemove,
    appointmentUpdate,
    appointmentList,
    appointmentTotal,
    appointmentInfo
}